<?php
/**
 * ACTIONS:
 * 1 - Switch field ON
 * 2 - Switch field OFF
 * 3 - Move field
 * 4 - $$$$$$$$$save field after editing
 *
 * sdate = 20
 */

$editor_mods = array('Textarea','Plane','Basic','Simple','Advanced');


if(session::user() && session::user()->id == '1')
{
   $nodeId = $FILTER->isValidInteger($REQUEST->getRequestLast()) ? $REQUEST->getRequestLast() : 0;
   $SMARTY->assign('c', $nodeId);
   $FIELDS = fields::getInstance();

   switch($action)
   {
      case 1:
      case 2:
      # SWITCH ON/OFF FIELD
         $respond = FALSE;
         if(!empty($id))
         {
            $F = $FIELDS->getField($id);
            if(is_object($F))
            {
               # OPERATE WITH FIELD
               if($nodeId != $F->node)
               {
                  $F = $FIELDS->copyField($id, $nodeId);
               }
               if($action == 1)
               {
                  $F->turnOn();
               }
               if($action == 2)
               {
                  $F->turnOff();
               }

               $respond = $F;
            } else
            {
               # OPERATE WITH TAB
            }
         }
         echo json_encode($respond);
         exit();
         break;



      case 3:
         # MOVE FIELD

         $e = FALSE;
         $node_patern = '/^(t|f)?\d+$/';
         if(!empty($_POST['node']) && preg_match($node_patern, $_POST['node']))
         {
            $node_type = substr($_POST['node'], 0, 1);
            $node_id   = substr($_POST['node'], 1);
         } else
         {
            $e = TRUE;
         }
         if(!empty($_POST['parent']) && preg_match($node_patern, $_POST['parent']))
         {
            $parent_type = substr($_POST['parent'], 0, 1);
            $parent_id   = substr($_POST['parent'], 1);
         } else
         {
            $e = TRUE;
         }
         if(!empty($_POST['relate']) && preg_match($node_patern, $_POST['relate']))
         {
            $relate_type = substr($_POST['relate'], 0, 1);
            $relate_id   = substr($_POST['relate'], 1);
         } else
         {
            $e = TRUE;
         }
         $position = !empty($_POST['position']) && in_array($_POST['position'], array('after', 'before', 'inside', 'first', 'last')) ? $_POST['position'] : FALSE;

         if(!empty($position) && !$e)
         {
            $e = FALSE;
            if(($node_type == 'f' && $parent_id != 0) || ($node_type == 't' && $parent_id == 0))
            {
               # ALLOWED MOVEMENTS: TAB UNDER THE ROOT OF A TREE, FIELD ONLY UNDER A TAB
               # OPERATE WITH FIELD
               $F = $FIELDS->getField($node_id);
               if(is_object($F))
               {
                  if($nodeId != $F->node)
                  {
                     $F = $FIELDS->copyField($node_id, $nodeId);
                  }

                  // TODO: calculate new position of a field.
                  $FIELDS->moveField($F, $parent_id, $position);
                  $e = TRUE;
               }
            }
         } else
         {
            $e = FALSE;
         }
         
         echo json_encode($e);
         exit();

         break;

         


      default:
         $SMARTY->assign('data', $FIELDS->getFields($nodeId));
         $layout = 'interface.html';
   }

   /*
   $fields = array();
 
   switch($action)
   {
      case 1:
      case 2:
         # RESET FIELDS
         $belong = false;
         if($action == 1) $belong =  'c';
         if($action == 2) $belong =  'a';

         if($FIELDS->resetFields($nodeId, $belong))
         {
            $params = array();
            $params['varkey'] = 'resetfields';
            if($nodeId) $params['varkey'] = 'resetfields_part';
            $ErrorHandler->setSuccess($params);
         } else
         {
            $ErrorHandler->setError();
         }
         unset($belong);
         break;

      case 3:
         # GET FIELD PARAMS FOR AJAX EDITING
         $respond = $FIELDS->ajaxEditor($id);
         echo json_encode($respond);
         exit();
         break;

      case 4:

         # CHANGE FIELD PARAMS
         $data = 0;
         if($id)
         {
            $c = isset($_GET['c']) && $FILTER->isValidInteger($_GET['c']) ? $_GET['c'] : 0; # Category ID

            $field          = array();
            $field['id']    = $id;
            $field['node']  = $c;
            $field['title'] = stripslashes($_GET['f']['title']);

            $params['value']  = isset($_GET['f']['value'])  ? $_GET['f']['value']  : false;
            $params['values'] = isset($_GET['f']['values']) ? $_GET['f']['values'] : false;
            $params['html']   = isset($_GET['f']['html'])   ? $_GET['f']['html']   : false;
            $params['height'] = isset($_GET['f']['height']) ? $_GET['f']['height'] : false;
            $params['emode']  = isset($_GET['f']['emode'])  ? $_GET['f']['emode']  : false;
            $params['min_w']  = isset($_GET['f']['min_w'])  ? $_GET['f']['min_w']  : false;
            $params['min_h']  = isset($_GET['f']['min_h'])  ? $_GET['f']['min_h']  : false;
            $params['max_w']  = isset($_GET['f']['max_w'])  ? $_GET['f']['max_w']  : false;
            $params['max_h']  = isset($_GET['f']['max_h'])  ? $_GET['f']['max_h']  : false;
            $params['fixed']  = isset($_GET['f']['ratio'])  ? $_GET['f']['ratio']  : false;

            $field['params'] = $params;

            $data = $FIELDS->changeParams($field);
         }
         echo json_encode($data);
         exit();
         break;

      default:
         if(isset($_POST['f']['type']) && in_array($_POST['f']['type'], array('a','c','u','s')))
         {
            $belong = $_POST['f']['type'];
            unset($_POST['f']['type']);
            foreach($_POST['f'] AS $id=>$f)
            {
               $field             = $f;
               $field['id']       = $FILTER->getFirstInteger($id);
               $field['position'] = $FILTER->getFirstInteger($field['position']);
               $field['tab']      = $FILTER->getFirstInteger($field['tab']);
               $field['belong']   = $belong;

               $FIELDS->saveField($field, $nodeId);
            }
            $FIELDS->commitFields();
         }
   }

   $SMARTY->assign('data', $FIELDS->getFields($nodeId));
   */
} else
{
   if (session::user())
   {
      $ErrorHandler->setError(
         array(
         'code' => 'access',
         'varkey' => 'access_denied'
         )
      );
   }
   $layout = 'login.html';
}